import axios, {type AxiosResponse} from "axios";
import {ElMessage} from "element-plus";

var axiosInstance = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL,
});

axiosInstance.interceptors.response.use((response: AxiosResponse) => {
    if (response.config.responseType === 'json' || response.config.responseType === undefined) {
        const res = response.data
        if (res.code == 1 || res.code == 200 || res.success) {
            return res.data
        } else {
            console.log(res);
            if (res.msg !== undefined) {
                ElMessage.error(res.msg)
                return Promise.reject(res.msg)
            } else if (res.message !== undefined) {
                ElMessage.error(res.message)
                return Promise.reject(res.message)
            }
        }
    } else {
        return response
    }
}, (error) => {
    ElMessage.error(error.response.data.message)
    return Promise.reject(error)
})


export default axiosInstance;
